﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using StudentLibrary.Module.Util;

namespace StudentLibrary
{
    public partial class FormDecommissionedBooks : Form
    {
        public FormDecommissionedBooks()
        {
            InitializeComponent();
            LoadContent();
        }
        public void LoadContent()
        {
            try
            {
                string sql = @"SELECT * 
                               FROM DecommissionedBooks";
                dataGridViewDecBooks.Rows.Clear();
                foreach (DataRow row in sql.SQLQueryAsDataTable().Rows)
                    dataGridViewDecBooks.Rows.Add(row["idBook"].ToString(), double.Parse(row["dateOfWrite_off"].ToString())
                                                                                  .UnixTimeStampToDateTime()
                                                                                  .ToString("dd/MM/yyyy\r\n"));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка: {ex.Message}");
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            new FormAddDecBooks().ShowDialog();
            LoadContent();
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            string searchText = textBoxSearch.Text.Trim();

            if (string.IsNullOrEmpty(searchText))
            {
                foreach (DataGridViewRow row in dataGridViewDecBooks.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        row.Visible = true;
                    }
                }
            }
            else
            {
                foreach (DataGridViewRow row in dataGridViewDecBooks.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        bool rowContainsSearchText = false;

                        foreach (DataGridViewCell cell in row.Cells)
                        {
                            if (cell.Value != null && cell.Value.ToString().IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0)
                            {
                                rowContainsSearchText = true;
                                break;
                            }
                        }

                        row.Visible = rowContainsSearchText;
                    }
                }
            }
        }
    }

}
